package com.luhaoran.hrm.controller;


import com.luhaoran.hrm.entity.Department;
import com.luhaoran.hrm.service.IDepartmentService;
import com.luhaoran.hrm.utils.ViewUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @author haoranlu
 * @project IntelliJ IDEA
 */
/**
 * 部门相关接口
 */

@RestController
@RequestMapping("department")
public class DepartmentController {
    @Resource
    HttpServletRequest request;
    @Resource
    HttpServletResponse response;
    @Resource
    IDepartmentService departmentService;

    /**
     * 部门列表
     * @return
     */
    @GetMapping
    public Object index() {
        List<Department> departments = departmentService.getList();
        return ViewUtil.json(response, 200, "success", departments);
    }

    /**
     * 获取单个部门
     * @return
     */
    @GetMapping("get")
    public Object get(@RequestParam("id") Integer id) {
        Department department = departmentService.getById(id);
        return ViewUtil.json(response, 200, "success", department);
    }

    /**
     * 保存部门
     * @return
     */
    @PostMapping("save")
    public Object save(@RequestBody Department department) {
        Integer departmentId = departmentService.saveDepartment(department);
        return ViewUtil.json(response, 200, "success", departmentId);
    }


    /**
     * 移除部门
     * @param id
     * @return
     */
    @GetMapping("remove/{id}")
    public Object remove(@PathVariable("id") Integer id) {
        departmentService.removeById(id);
        return ViewUtil.json(response, 200, "success");
    }


}

